
TERMINAL_DIR = ./terminal
TERMINAL_JAVA = \
    $(TERMINAL_DIR)/BareBonesBrowserLaunch.java \
    $(TERMINAL_DIR)/Terminal.java \
    $(TERMINAL_DIR)/Kermit.java

SCHEME_DIR = ./console/scm
SCHEME_JAVA = \
	$(SCHEME_DIR)/Scheme.java

MANAGER_DIR = ./console/manager
MANAGER_JAVA = \
	$(MANAGER_DIR)/TaskPanel.java \
	$(MANAGER_DIR)/TaskManager.java \
	$(MANAGER_DIR)/ScmTaskProcess.java \
	$(MANAGER_DIR)/TaskProcess.java

WINDOW_DIR = ./console/window
WINDOW_JAVA = \
	$(WINDOW_DIR)/SwingMessageArea.java \
	$(WINDOW_DIR)/SwingInPort.java

UTILS_DIR = ./console
UTILS_JAVA = \
	$(UTILS_DIR)/Utilities.java \
	$(UTILS_DIR)/ActionSet.java \
	$(UTILS_DIR)/ActionProperties.java \
	$(UTILS_DIR)/ConsoleAction.java

CONSOLE_DIR = ./console
CONSOLE_JAVA = \
	$(CONSOLE_DIR)/AppLock.java \
	$(CONSOLE_DIR)/ShellEnvironment.java \
	$(CONSOLE_DIR)/ClassPathHacker.java \
	$(CONSOLE_DIR)/ConsoleException.java

SCM_LIB_DIR = ./scripts/lib
SCM_LIB = \
	$(SCM_LIB_DIR)/db.scm \
	$(SCM_LIB_DIR)/action-set.scm \
	$(SCM_LIB_DIR)/task-manager.scm \
	$(SCM_LIB_DIR)/console.scm \
	$(SCM_LIB_DIR)/terminal.scm

RXTX_DIR = ./gnu/io
RXTX_JAVA = \
	$(RXTX_DIR)/CommDriver.java \
	$(RXTX_DIR)/CommPort.java \
	$(RXTX_DIR)/CommPortEnumerator.java \
	$(RXTX_DIR)/CommPortIdentifier.java \
	$(RXTX_DIR)/CommPortOwnershipListener.java \
	$(RXTX_DIR)/Configure.java \
	$(RXTX_DIR)/I2C.java \
	$(RXTX_DIR)/I2CPort.java \
	$(RXTX_DIR)/I2CPortEvent.java \
	$(RXTX_DIR)/I2CPortEventListener.java \
	$(RXTX_DIR)/LPRPort.java \
	$(RXTX_DIR)/NoSuchPortException.java \
	$(RXTX_DIR)/ParallelPort.java \
	$(RXTX_DIR)/ParallelPortEvent.java \
	$(RXTX_DIR)/ParallelPortEventListener.java \
	$(RXTX_DIR)/PortInUseException.java \
	$(RXTX_DIR)/RS485.java \
	$(RXTX_DIR)/RS485Port.java \
	$(RXTX_DIR)/RS485PortEvent.java \
	$(RXTX_DIR)/RS485PortEventListener.java \
	$(RXTX_DIR)/RXTXCommDriver.java \
	$(RXTX_DIR)/RXTXPort.java \
	$(RXTX_DIR)/RXTXVersion.java \
	$(RXTX_DIR)/Raw.java \
	$(RXTX_DIR)/RawPort.java \
	$(RXTX_DIR)/RawPortEvent.java \
	$(RXTX_DIR)/RawPortEventListener.java \
	$(RXTX_DIR)/SerialPort.java \
	$(RXTX_DIR)/SerialPortEvent.java \
	$(RXTX_DIR)/SerialPortEventListener.java \
	$(RXTX_DIR)/UnSupportedLoggerException.java \
	$(RXTX_DIR)/UnsupportedCommOperationException.java \
	$(RXTX_DIR)/Zystem.java

lib/jfreechart-1.0.13.tar.gz:
	wget --no-check-certificate "https://downloads.sourceforge.net/project/jfreechart/1.%20JFreeChart/1.0.13/jfreechart-1.0.13.tar.gz?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fjfreechart%2Ffiles%2F&ts=1284587115&use_mirror=voxel" -O $@

lib/jfreechart-1.0.13.jar: lib/jfreechart-1.0.13.tar.gz
	tar xOvfz lib/jfreechart-1.0.13.tar.gz jfreechart-1.0.13/lib/jfreechart-1.0.13.jar > $@

lib/jcommon-1.0.16.jar: lib/jfreechart-1.0.13.tar.gz
	tar xOvfz lib/jfreechart-1.0.13.tar.gz jfreechart-1.0.13/lib/jcommon-1.0.16.jar > $@


#	wget --no-check-certificate "http://downloads.sourceforge.net/project/jdatepicker/jDatePicker%20Jar/1.3.x/jdatepicker-1.3.2.jar?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjdatepicker%2Ffiles%2F&ts=1284698449&use_mirror=superb-sea2" -O $@

lib/jdatepicker-1.3.2.jar: lib/JDatePicker-1.3.2-dist.tar.gz
	tar xOvfz lib/JDatePicker-1.3.2-dist.tar.gz JDatePicker-1.3.2-dist/jdatepicker-1.3.2.jar > $@

lib/jta26.jar:
	wget http://javassh.org/download/jta26.jar -O $@

lib/kawa-1.13.jar:
	-mkdir lib
	wget ftp://ftp.gnu.org/pub/gnu/kawa/kawa-1.13.jar -O $@

lib/htmlunit-2.8.zip:
	wget --no-check-certificate "https://downloads.sourceforge.net/project/htmlunit/htmlunit/2.8/htmlunit-2.8.zip?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fhtmlunit%2Ffiles%2F&ts=1284588631&use_mirror=cdnetworks-us-1" -O $@

lib/JDatePicker-1.3.2-dist.tar.gz:
	wget --no-check-certificate "http://downloads.sourceforge.net/project/jdatepicker/Releases/1.3.x/JDatePicker-1.3.2-dist.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjdatepicker%2Ffiles%2FReleases%2F1.3.x%2F&ts=1322955394&use_mirror=superb-dca2" -O $@

lib/htmlunit-2.8.jar: lib/htmlunit-2.8.zip
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/xalan-2.7.1.jar > lib/xalan-2.7.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/serializer-2.7.1.jar > lib/serializer-2.7.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/xml-apis-1.3.04.jar > lib/xml-apis-1.3.04.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/commons-collections-3.2.1.jar > lib/commons-collections-3.2.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/commons-lang-2.4.jar > lib/commons-lang-2.4.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/httpclient-4.0.1.jar > lib/httpclient-4.0.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/httpcore-4.0.1.jar > lib/httpcore-4.0.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/commons-logging-1.1.1.jar > lib/commons-logging-1.1.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/commons-codec-1.4.jar > lib/commons-codec-1.4.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/httpmime-4.0.1.jar > lib/httpmime-4.0.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/apache-mime4j-0.6.jar > lib/apache-mime4j-0.6.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/htmlunit-core-js-2.8.jar > lib/htmlunit-core-js-2.8.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/xercesImpl-2.9.1.jar > lib/xercesImpl-2.9.1.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/nekohtml-1.9.14.jar > lib/nekohtml-1.9.14.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/cssparser-0.9.5.jar > lib/cssparser-0.9.5.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/sac-1.3.jar > lib/sac-1.3.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/commons-io-1.4.jar > lib/commons-io-1.4.jar
	unzip -p lib/htmlunit-2.8.zip htmlunit-2.8/lib/htmlunit-2.8.jar > lib/htmlunit-2.8.jar

JAVA_LIBRARIES = \
    lib/kawa-1.13.jar \
    lib/jdatepicker-1.3.2.jar \
    lib/jcommon-1.0.16.jar \
    lib/jfreechart-1.0.13.jar \
    lib/jta26.jar \
    lib/htmlunit-2.8.jar \
    lib/xalan-2.7.1.jar \
    lib/serializer-2.7.1.jar \
    lib/xml-apis-1.3.04.jar \
    lib/commons-collections-3.2.1.jar \
    lib/commons-lang-2.4.jar \
    lib/httpclient-4.0.1.jar \
    lib/httpcore-4.0.1.jar \
    lib/commons-logging-1.1.1.jar \
    lib/commons-codec-1.4.jar \
    lib/httpmime-4.0.1.jar \
    lib/apache-mime4j-0.6.jar \
    lib/htmlunit-core-js-2.8.jar \
    lib/xercesImpl-2.9.1.jar \
    lib/nekohtml-1.9.14.jar \
    lib/cssparser-0.9.5.jar \
    lib/sac-1.3.jar \
    lib/commons-io-1.4.jar

#    lib/RXTXcomm.jar

    
UNAME = $(shell uname)

JAVA_CLASSPATH = \
	".$(SEP)lib/kawa-1.13.jar$(SEP)lib/jcommon-1.0.16.jar$(SEP)lib/jfreechart-1.0.13.jar$(SEP)lib/RXTXcomm.jar$(SEP)lib/jta26.jar$(SEP)lib/htmlunit-2.8.jar$(SEP)lib/xalan-2.7.1.jar$(SEP)lib/serializer-2.7.1.jar$(SEP)lib/xml-apis-1.3.04.jar$(SEP)lib/commons-collections-3.2.1.jar$(SEP)lib/commons-lang-2.4.jar$(SEP)lib/httpclient-4.0.1.jar$(SEP)lib/httpcore-4.0.1.jar$(SEP)lib/commons-logging-1.1.1.jar$(SEP)lib/commons-codec-1.4.jar$(SEP)lib/httpmime-4.0.1.jar$(SEP)lib/apache-mime4j-0.6.jar$(SEP)lib/htmlunit-core-js-2.8.jar$(SEP)lib/xercesImpl-2.9.1.jar$(SEP)lib/nekohtml-1.9.14.jar$(SEP)lib/cssparser-0.9.5.jar$(SEP)lib/sac-1.3.jar$(SEP)lib/commons-io-1.4.jar"

ifeq "$(UNAME)" "Linux"
LIBTSC=libtsc.so
libtsc.so: $(TERMINAL_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(TERMINAL_JAVA)
	javah -classpath $(JAVA_CLASSPATH) terminal.Terminal
	cc -I. -I$(JAVA_HOME)/include -I$(JDK_DIR)/include/linux -shared -fPIC -o $(LIBTSC) native/tsc.c 

LIBRXTX=librxtx.so
librxtx.so: $(RXTX_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(RXTX_JAVA)
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXPort
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXVersion
	cc -I. -I$(JAVA_HOME)/include -I$(JDK_DIR)/include/linux -shared -fPIC -o $(LIBRXTX) native/SerialImp.c native/fixup.c

#	cc -I. -shared -fPIC -o $(LIBRXTX) native/SerialImp.c 
else

ifeq "$(UNAME)" "Darwin"

LIBTSC=libtsc.so
libtsc.so: $(TERMINAL_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(TERMINAL_JAVA)
	javah -classpath $(JAVA_CLASSPATH) terminal.Terminal
	cc -I. -I/System/Library/Frameworks/JavaVM.framework/Headers -g -O2 -DBSD_SOURCE -bundle -c native/tsc.c -o tsc.o
	cc -bundle -framework CoreFoundation -framework IOKit -o $(LIBTSC) tsc.o

LIBRXTX=librxtx.so
librxtx.so: $(RXTX_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(RXTX_JAVA)
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXPort
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXVersion
	cc -I. -I/System/Library/Frameworks/JavaVM.framework/Headers -g -O2 -DBSD_SOURCE -DDISABLE_LOCKFILES -bundle -c native/SerialImp.c -o SerialImp.o
	cc -bundle -framework CoreFoundation -framework IOKit -o $(LIBRXTX) SerialImp.o

else

LIBTSC=tsc.dll
tsc.dll: $(TERMINAL_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(TERMINAL_JAVA)
	javah -classpath $(JAVA_CLASSPATH) terminal.Terminal
	cc -mno-cygwin -I. -I$(JDK_DIR)/include -I$(JDK_DIR)/include/win32 -Wl,--add-stdcall-alias -shared -o $(LIBTSC) native/tsc.c 

LIBRXTX=rxtx.dll
rxtx.dll: native/SerialImp.c $(RXTX_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(RXTX_JAVA)
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXPort
	javah -classpath $(JAVA_CLASSPATH) gnu.io.RXTXVersion
	cc -D__int64="long long" -mno-fp-ret-in-387 -mno-cygwin -I. -I$(JDK_DIR)/include -I$(JDK_DIR)/include/win32 -Wl,--add-stdcall-alias -luser32 -lgdi32 -lcomdlg32 -lkernel32 -ladvapi32 -lmingw32 -lmoldname -lcrtdll -shared -o $(LIBRXTX) native/SerialImp.c  native/init.c native/fixup.c native/termios.c

#	cc -mno-cygwin -I. -I$(JDK_DIR)/include -I$(JDK_DIR)/include/win32 -Wl,--add-stdcall-alias -shared -o $(LIBTSC) native/SerialImp.c 

endif
endif

console.jar: s-manifest  $(JAVA_LIBRARIES) $(LIBRXTX) $(LIBTSC) $(MANAGER_JAVA) $(WINDOW_JAVA) $(CONSOLE_JAVA) $(SCM_LIB) $(SCHEME_JAVA) $(UTILS_JAVA) $(WEB2_JAVA) $(TERMINAL_JAVA) $(RXTX_JAVA)
	javac -cp $(JAVA_CLASSPATH) $(MANAGER_JAVA) $(UTILS_JAVA) $(WINDOW_JAVA) $(WEB2_JAVA) console/ConsoleException.java $(TERMINAL_JAVA) $(RXTX_JAVA)
	java -cp $(JAVA_CLASSPATH) kawa.repl --no-warn-invoke-unknown-method        -P console.scm. -C scripts/lib/task-manager.scm
	java -cp $(JAVA_CLASSPATH) kawa.repl --no-warn-invoke-unknown-method        -P console.scm. -C scripts/lib/console.scm
	java -cp $(JAVA_CLASSPATH) kawa.repl --no-warn-invoke-unknown-method        -P console.scm. -C scripts/lib/action-set.scm
	java -cp $(JAVA_CLASSPATH) kawa.repl --no-warn-invoke-unknown-method        -P console.scm. -C scripts/lib/terminal.scm
	javac -cp $(JAVA_CLASSPATH) $(CONSOLE_JAVA) $(SCHEME_JAVA)
	jar cfm console.jar s-manifest \
		icons/*.png \
		icons/*.ico \
		$(RXTX_DIR)/*.class \
		$(TERMINAL_DIR)/*.class \
		$(SCHEME_DIR)/*.class \
		$(MANAGER_DIR)/*.class \
		$(WINDOW_DIR)/*.class \
		$(CONSOLE_DIR)/*.class \
		$(LIBTSC) \
		$(LIBRXTX)

repl: console.jar
	java -jar console.jar

